<?php

class Admin_Form_Adv_Add extends My_FormAdmin {

    public function init() {
        $this->setTitle($this->translate('XXX_ADMIN_ADV_ADD_FORM_TITLE'));
        $this->setMethod('post');
        $this->setAction($this->getView()->url(array('controller' => 'adv', 'action' => 'save'), 'admin', true));

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel(_('XXX_ADMIN_ADV_FORM_NAME_LABEL'));
        $name->setRequired();
        $this->addElement($name);

        $place = new Zend_Form_Element_Select('place');
        $place->setLabel(_('XXX_ADMIN_ADV_FORM_PLACE_LABEL'));
        $place->setRequired();
        $place->setMultiOptions(array(
            Application_Model_DAO_Adv::PLACE_TOP => _('XXX_ADMIN_ADV_FORM_PLACE_TOP_TEXT'),
            Application_Model_DAO_Adv::PLACE_BOTTOM => _('XXX_ADMIN_ADV_FORM_PLACE_BOTTOM_TEXT'),
            Application_Model_DAO_Adv::PLACE_RIGHT => _('XXX_ADMIN_ADV_FORM_PLACE_RIGHT_TEXT'),
            Application_Model_DAO_Adv::PLACE_LEFT => _('XXX_ADMIN_ADV_FORM_PLACE_LEFT_TEXT'),
        ));
        $this->addElement($place);

        $type = new Zend_Form_Element_Select('type');
        $type->setLabel(_('XXX_ADMIN_ADV_FORM_TYPE_LABEL'));
        $type->setRequired();
        $type->setMultiOptions(array(
            '' => '',
            Application_Model_DAO_Adv::TYPE_IMG => _('XXX_ADMIN_ADV_FORM_TYPE_IMG_TEXT'),
            Application_Model_DAO_Adv::TYPE_HTML => _('XXX_ADMIN_ADV_FORM_TYPE_HTML_TEXT'),
        ));
        $this->addElement($type);

        // html
        $html = new Zend_Form_Element_Textarea('html');
        $html->setLabel(_('XXX_ADMIN_ADV_FORM_HTML_LABEL'));
        $this->addElement($html);

        // img
        $img = new My_Form_Element_ImageUpload('img');
        $img->setLabel(_('XXX_ADMIN_ADV_FORM_IMG_LABEL'));
        $img->setSourceName('adv');
        $img->setAttrib('class', 'element-big');
        $this->addElement($img);

        $url = new Zend_Form_Element_Text('url');
        $url->setLabel(_('XXX_ADMIN_ADV_FORM_URL_LABEL'));
        $this->addElement($url);

        $this->addSubmitElement();

        $dynamic = new My_Form_Element_DynamicElements($type, array(
            Application_Model_DAO_Adv::TYPE_IMG => array($img, $url),
            Application_Model_DAO_Adv::TYPE_HTML => array($html),
        ));
        $this->addElement($dynamic);
        
    }

    public function getValues($suppressArrayNotation = false) {
        $values = parent::getValues($suppressArrayNotation);
        $type = $values['type'];
        if ($type == Application_Model_DAO_Adv::TYPE_IMG) {
            $values['content'] = Application_Model_DAO_Adv::IMG_PREFIX . $values['img'];
        } elseif ($type == Application_Model_DAO_Adv::TYPE_HTML) {
            $values['content'] = $values['html'];
            $values['url'] = null;
        }
        unset($values['img']);
        unset($values['html']);
        return $values;
    }

}

